<?php

defined('SYSPATH') or die('Access error!');

class Controller_Message extends Controller_Main
{

    private $message;

    public function before()
    {
        parent::before();
        $this->message = Model::factory('user_message');
        if (!$this->logged_in())
        {
            Message::error('请先登录');
            $this->request->redirect(Route::get('user')->uri(array('action' => 'login')));
        }
    }

    public function action_add()
    {
        if ($_POST)
        {
            if (!$this->message->validate($_POST))
            {
                Message::error('提交的数据不正确');
            }
            else
            {
                $_POST['from_user'] = $this->user_detail['id'];
                if ($this->message->send_message($_POST))
                {
                    Message::success('请静待回复');
                }
            }
            if ($_POST['url'] != '')
            {
                $this->request->redirect(URL::site($_POST['url']));
            }
            else
            {
                $this->request->redirect(URL::site());
            }
        }
    }

}